package com.frxuexi.park2209_2.video.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.bean.VideoBeanItem
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.park2209_2.R
import com.frxuexi.park2209_2.databinding.ActivityVideoDetailBinding
import com.frxuexi.park2209_2.video.adapter.VideoCommentAdapter
import com.frxuexi.park2209_2.video.viewmodel.VideoViewModel
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/VideoDetailActivity")
@AndroidEntryPoint
class VideoDetailActivity : BaseActivity<ActivityVideoDetailBinding, VideoViewModel>() {

    @Autowired
    lateinit var video: VideoBeanItem
    lateinit var gsy: StandardGSYVideoPlayer
    val videoCommentAdapter by lazy { VideoCommentAdapter() }
    override fun initData() {
        vm.videoCommentSuc.observe(this) {
            Log.i("===", it.toString())
            videoCommentAdapter.addData(it)
            videoCommentAdapter.notifyDataSetChanged()
        }

        vm.commentSuc.observe(this) {
            if (it.isNotEmpty()) {
                videoCommentAdapter.addData(0, it)
                videoCommentAdapter.notifyItemInserted(0)
            }
        }
        if(video.dianzan==0){
            binding.videoZan.setImageResource(R.drawable.ic_action_nozan)
        }else{
            binding.videoZan.setImageResource(R.drawable.ic_action_zan)
        }
        vm.zanSuc.observe(this){
            video.dianzan=1
            binding.videoZan.setImageResource(R.drawable.ic_action_zan)
        }

        vm.nozanSuc.observe(this){
            video.dianzan=0
            ToastUtils.showLong("取消成功")
            binding.videoZan.setImageResource(R.drawable.ic_action_nozan)
        }
        if(video.guanzhu==0){
            binding.videoGz.setImageResource(R.drawable.ic_action_nozan)
        }else{
            binding.videoGz.setImageResource(R.drawable.ic_action_zan)
        }
        vm.guanzhuSuc.observe(this){
            video.guanzhu=1
            binding.videoGz.setImageResource(R.drawable.ic_action_zan)
        }

        vm.noguanzhuSuc.observe(this){
            video.guanzhu=0
            binding.videoGz.setImageResource(R.drawable.ic_action_nozan)
        }

    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        Log.i("===",video.toString())
        gsy = binding.gsy

        gsy.setUp(video.videopath, true, video.caption)
        gsy.startPlayLogic()

        binding.rvComment.apply {
            adapter = videoCommentAdapter
            layoutManager = LinearLayoutManager(this@VideoDetailActivity)
        }

        vm.getVideoComment(video.id)

        binding.btnSend.setOnClickListener {
            val comment = binding.etComment.text.toString()
            if (comment.isNullOrEmpty()) {
                ToastUtils.showLong("评论不能为空")
                return@setOnClickListener
            }
            vm.comment(mapOf("videoId" to video.id, "msg" to comment))
        }

        binding.videoZan.setOnClickListener {
            if(video.dianzan==0){
                vm.love(video.id)
            }else{
                vm.nolove(video.id)
            }

        }

        binding.videoGz.setOnClickListener {
            if(video.guanzhu==0) {
                vm.guanzhu(mapOf("authname" to video.authname))
            }else{//取消关注
                vm.noguanzhu(mapOf("authname" to video.authname))
            }
        }
    }

    override fun onPause() {
        super.onPause()
        gsy.onVideoPause()
    }

    override fun onStop() {
        super.onStop()
//        gsy.
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
    }

}